package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.Constants;
import com.taotao.common.utils.DateUtil;
import com.taotao.common.utils.RedisUtils;
import com.taotao.mapper.EyParamMapper;
import com.taotao.pojo.EyParam;
import com.taotao.pojo.EyParamExample;
import com.taotao.service.ParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Service
public class ParamServiceImpl implements ParamService {

	BaseResult baseResult = new BaseResult();
	@Autowired
	EyParamMapper eyParamMapper;

	@Override
	public Map<String, Object> getParam(Map<String, String> map) {
		try {
			String active = this.getResourceParam("active");
			String paramKey = map.get("paramKey");
			EyParamExample example = new EyParamExample();
			com.taotao.pojo.EyParamExample.Criteria criteria = example.createCriteria();
			criteria.andDomainEqualTo(active);
			if (StringUtils.isNotBlank(paramKey)) {
				criteria.andParamKeyEqualTo(paramKey);
			}
			List<EyParam> list = eyParamMapper.selectByExample(example);

			HashMap<String, String> returnMap = new HashMap<String, String>();
			for (EyParam p : list) {
				returnMap.put(p.getParamKey(), p.getParamValue());
				RedisUtils.hSet(Constants.paramMap, p.getParamKey(), p.getParamValue());
			}

			return baseResult.getSuccMap(returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String getParam(String paramKey) {
		try {
			String active = this.getResourceParam("active");
			EyParamExample example = new EyParamExample();
			com.taotao.pojo.EyParamExample.Criteria criteria = example.createCriteria();
			criteria.andDomainEqualTo(active);
			criteria.andParamKeyEqualTo(paramKey);
			List<EyParam> listPic = eyParamMapper.selectByExample(example);
			EyParam eyParam = listPic.get(0);
			return eyParam.getParamValue();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Map<String, Object> modifyParam(Map<String, String> map) {
		try {
			String paramId = map.get("paramId");
			String paramValue = map.get("paramValue");
			String details = map.get("details");
			EyParam param = eyParamMapper.selectByPrimaryKey(Integer.parseInt(paramId));
			param.setParamValue(paramValue);
			param.setDetails(details);
			param.setUpdateTime(DateUtil.getDateAndTime());
			eyParamMapper.updateByPrimaryKeySelective(param);
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

	@Override
	public Map<String, Object> queryParamList(Map<String, String> map) {
		String active = this.getResourceParam("active");
		String paramKey = map.get("paramKey");
		String paramValue = map.get("paramValue");
		String details = map.get("details");
		EyParamExample example = new EyParamExample();
		com.taotao.pojo.EyParamExample.Criteria criteria = example.createCriteria();
		criteria.andDomainEqualTo(active);
		if (StringUtils.isNotBlank(paramKey)) {
			criteria.andParamKeyEqualTo(paramKey);
		}
		if (StringUtils.isNotBlank(paramValue)) {
			criteria.andParamValueEqualTo(paramValue);
		}
		if (StringUtils.isNotBlank(details)) {
			criteria.andDetailsLike("%" + details + "%");
		}
		example.setOrderByClause(" id desc ");
		String pageNum = map.get("pageNum");
		String pageSize = map.get("pageSize");
		Integer num = StringUtils.isBlank(pageNum) ? Constants.pageNum : Integer.parseInt(pageNum);
		Integer size = StringUtils.isBlank(pageSize) ? Constants.pageSize : Integer.parseInt(pageSize);

		PageHelper.startPage(num, size);
		List<EyParam> list = eyParamMapper.selectByExample(example);
		PageInfo<EyParam> pageInfo = new PageInfo<>(list);
		return baseResult.getSuccMap(pageInfo);
	}

	public static String getResourceParam(String key) {
		try {
			InputStream in;
			Properties prop = new Properties();
			File file = ResourceUtils.getFile("classpath:/resource.properties");
			in = new BufferedInputStream(new FileInputStream(file));
			prop.load(in);     ///加载属性列表
			in.close();
			return String.valueOf(prop.get(key));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Map<String, Object> blackContentCheck(String title) {
		try {
			if(StringUtils.isNotBlank(title)){
				String blackContent = this.getParam(Constants.black_content);
				Boolean flag = false;
				String[] blackList = blackContent.split("\\|");
				for (String black : blackList) {
					if (title.toLowerCase().contains(black.toLowerCase())) {
						flag = true;
						break;
					}
				}
				if (flag) {
					StringBuffer sb = new StringBuffer();
					String  blackListString= StringUtils.join(blackList,",");
					sb.append("Sensitive words:"+blackListString);
					return baseResult.getErrorMap(sb.toString());
				}
			}
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			//异常返回成功
			return baseResult.getSuccMap();
		}
	}

}
